----------------------------------------------------------------------------------------------------------------------------------
--[[



]]--
----------------------------------------------------------------------------------------------------------------------------------
--- 界面组件
    local Widget = require "widgets/widget"
    local Image = require "widgets/image" -- 引入image控件
    local UIAnim = require "widgets/uianim"
    local Screen = require "widgets/screen"
    local AnimButton = require "widgets/animbutton"
    local ImageButton = require "widgets/imagebutton"
    local Menu = require "widgets/menu"
    local Text = require "widgets/text"
    local TEMPLATES = require "widgets/redux/templates"
----------------------------------------------------------------------------------------------------------------------------------
---
    local function CreateText(inst)
        if not (ThePlayer and ThePlayer.HUD and ThePlayer.HUD.controls and TheInput )then
            return
        end
        -- local front_root = ThePlayer.HUD.controls
        ---------------------------------------------------------------------
        --- 前置根节点和组件
            local front_root = ThePlayer.HUD
            local self = inst.replica.loramia_com_power_battery or inst.replica._.loramia_com_power_battery
        ---------------------------------------------------------------------
        ---
            if self.__text_indicator and self.__text_indicator.inst:IsValid() then
                self.__text_indicator:Kill()
            end
        ---------------------------------------------------------------------
        --- 根节点
            local root = front_root:AddChild(Widget())
            root:SetHAnchor(1) -- 设置原点x坐标位置，0、1、2分别对应屏幕中、左、右
            root:SetVAnchor(2) -- 设置原点y坐标位置，0、1、2分别对应屏幕中、上、下
            root:SetScaleMode(SCALEMODE_FIXEDSCREEN_NONDYNAMIC)   --- 缩放模式
            root:SetCanFadeAlpha(true)
        ---------------------------------------------------------------------
        --- 
            self.__text_indicator = root
        ---------------------------------------------------------------------
        --- 文本
            local text_color = self:GetTextColour()
            local txt_box = root:AddChild(Text(CODEFONT,30,"100/100",text_color or {  0/255 , 255/255 ,255/255 , 1}))
            root.txt_box = txt_box
        ---------------------------------------------------------------------
        --- 加载范围外的时候直接隐藏
            root.inst:ListenForEvent("entitysleep",function()
                root:Hide()
            end,inst)
        ---------------------------------------------------------------------
        --- update 函数
            local offset_y = -40
            local function location_update_fn()
                ---------------------------------------------------------------------
                --- 客制化刷新函数
                    if self.text_box_custom_update_fn then
                        self.text_box_custom_update_fn(inst,root)
                        return
                    end
                ---------------------------------------------------------------------
                local radius = self:GetRadius() or 5
                local radius_sq = radius*radius
                local dis_sq = ThePlayer:GetDistanceSqToInst(inst)
                if dis_sq < radius_sq then
                    local s_pt_x,s_pt_y= TheSim:GetScreenPos(inst.Transform:GetWorldPosition()) -- 左下角为原点。
                    -- print("player in screen",s_pt_x,s_pt_y)
                    root:SetPosition(s_pt_x,s_pt_y+offset_y,0)
                    root:Show()
                    ------------------------------------------------------------------------------
                    --- 设置显示的内容
                        local current = self:GetCurrent() or 0
                        local max = self:GetMax() or 100
                        local current_str = string.format("%.1f",current)
                        local max_str = string.format("%d",max)
                        txt_box:SetString(current_str.."/"..max_str..".0")
                    ------------------------------------------------------------------------------
                    --- 设置透明度
                        local alpha = (1 - (dis_sq / radius_sq))*1.2
                        alpha = math.clamp(alpha,0,1)
                        root:SetFadeAlpha(alpha)
                    ------------------------------------------------------------------------------
                    root:MoveToBack()
                else
                    root:Hide()
                end
            end
            location_update_fn()
            TheInput:Loramia_Add_Update_Modify_Fn(root.inst,location_update_fn)
        ---------------------------------------------------------------------
        --- 跟随删除
            root.inst:ListenForEvent("onremove",function()
                root:Kill()
            end,inst)
        ---------------------------------------------------------------------
    end
----------------------------------------------------------------------------------------------------------------------------------
local loramia_com_power_battery = Class(function(self, inst)
    self.inst = inst

    ---------------------------------------------------------------------------------------------------
    --- 半径
        self.radius = 10
        self.__net_radius = net_float(inst.GUID,"loramia_com_power_battery.radius","loramia_com_power_battery.radius")
        self.inst:ListenForEvent("loramia_com_power_battery.radius",function()
            self.radius = self.__net_radius:value()
        end)
    ---------------------------------------------------------------------------------------------------
    --- current/max
        self.current = 0
        self.max = 100
        self.__net_current = net_float(inst.GUID,"loramia_com_power_battery.current","loramia_com_power_battery.update")
        self.__net_max = net_float(inst.GUID,"loramia_com_power_battery.max","loramia_com_power_battery.update")
        self.inst:ListenForEvent("loramia_com_power_battery.update",function()
            self.current = self.__net_current:value()
            self.max = self.__net_max:value()
        end)
    ---------------------------------------------------------------------------------------------------
    --- 屏蔽所有链接设备
        self.ignore_all_devices = false
        self.__net_ignore_all_devices = net_bool(inst.GUID,"loramia_com_power_battery.ignore_all_devices","loramia_com_power_battery.ignore_all_devices")
        self.inst:ListenForEvent("loramia_com_power_battery.ignore_all_devices",function()
            self.ignore_all_devices = self.__net_ignore_all_devices:value()
        end)
    ---------------------------------------------------------------------------------------------------
    --- 仅限客户端,激活指示器 . TheWorld:PushEvent("loramia_com_power_battery_indicator_active",placer_inst)
        if not TheNet:IsDedicated() then
            inst:ListenForEvent("loramia_com_power_battery_indicator_active", function(_,placer_inst)               
                ------------------------------------------------------------------------------------
                --- 屏蔽所有链接设备时候指示器不显示
                    if self.ignore_all_devices then
                        return
                    end
                ------------------------------------------------------------------------------------
                --- 控制表
                    --[[
                        做自适应兼容，还有call_back
                        placer_inst 可以是 inst ，
                        也可以是 { 
                            placer = inst ,
                            call_back = {
                                in_battery_area = {},
                            }
                        }
                    ]]--
                    local cmd_table = nil
                    if placer_inst.components == nil then
                        cmd_table = placer_inst
                        placer_inst = cmd_table.placer
                        cmd_table.call_back = cmd_table.call_back or {}
                        cmd_table.call_back.in_battery_area = cmd_table.call_back.in_battery_area or {}
                    end
                    if cmd_table then
                        placer_inst:DoPeriodicTask(FRAMES*3,function()
                            cmd_table.call_back.in_battery_area[inst] = inst:GetDistanceSqToInst(placer_inst) <= self.radius*self.radius
                        end)
                    end
                ------------------------------------------------------------------------------------
                --- 激活指示器
                    if placer_inst then
                        self:ActiveIndicator()
                        placer_inst:ListenForEvent("onremove",function()
                            self:DeactiveIndicator()
                        end)
                    end
                ------------------------------------------------------------------------------------
            end,TheWorld)
        end
    ---------------------------------------------------------------------------------------------------
    --- 电量指示器
        self.___text_indicator_respawn_net = net_entity(inst.GUID,"indicator_respawn","indicator_respawn")
        if not TheNet:IsDedicated() then
            self.inst:DoTaskInTime(3,CreateText)
            self.inst:ListenForEvent("indicator_respawn",CreateText)
        end
        if TheWorld.ismastersim then
            inst:ListenForEvent("ms_playerjoined",function(_,player)
                inst:DoTaskInTime(3,function()
                    self.___text_indicator_respawn_net:set(player)                    
                end)
            end,TheWorld)
        end
    ---------------------------------------------------------------------------------------------------   
end)
------------------------------------------------------------------------------------------------------------------------------
--- 发电半径
    function loramia_com_power_battery:SetRadius(radius)
        self.radius = radius
        if TheWorld.ismastersim then
            self.__net_radius:set(radius)
        end
    end
    function loramia_com_power_battery:GetRadius()
        return self.radius
    end
------------------------------------------------------------------------------------------------------------------------------
--- current/max
    function loramia_com_power_battery:SetCurrent(current)
        self.current = current
        if TheWorld.ismastersim then
            self.__net_current:set(current)
        end
    end
    function loramia_com_power_battery:GetCurrent()
        return self.current
    end
    function loramia_com_power_battery:SetMax(max)
        self.max = max
        if TheWorld.ismastersim then
            self.__net_max:set(max)
        end
    end
    function loramia_com_power_battery:GetMax()
        return self.max
    end
    function loramia_com_power_battery:GetPercent()
        return self.current / self.max
    end
    function loramia_com_power_battery:Has(value)
        return self.current >= value
    end
    function loramia_com_power_battery:IsEmpty()
        return self.current <= 0
    end
    function loramia_com_power_battery:IsFull()
        return self.current >= self.max
    end
------------------------------------------------------------------------------------------------------------------------------
--- 屏蔽所有链接设备
    function loramia_com_power_battery:Set_Ignore_All_Devices(flag)
        self.ignore_all_devices = flag
        if TheWorld.ismastersim then
            self.__net_ignore_all_devices:set(flag)
        end
    end
------------------------------------------------------------------------------------------------------------------------------
--- 指示器
    function loramia_com_power_battery:ActiveIndicator()
        if self.indicator and self.indicator:IsValid() then
            self.indicator:Remove()
        end
        local indicator = SpawnPrefab("loramia_sfx_dotted_circle_client")
        indicator:PushEvent("Set",{
            pt = Vector3(self.inst.Transform:GetWorldPosition()),
            radius = self:GetRadius(),
        })
        self.indicator = indicator
        indicator:ListenForEvent("onremove",function()
            indicator:Remove()
        end,self.inst)
        indicator.AnimState:SetAddColour(0, 1, 1, 0)
        indicator.AnimState:SetMultColour(0, 1, 1, 1)
        indicator.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
    end
    function loramia_com_power_battery:DeactiveIndicator()
        if self.indicator and self.indicator:IsValid() then
            self.indicator:Remove()
            self.indicator = nil
        end
    end
------------------------------------------------------------------------------------------------------------------------------
--- txt
    function loramia_com_power_battery:SetTextColour(r,g,b)
        --- r、g、b  0-1
        self.text_colour = {r,g,b,1}
    end
    function loramia_com_power_battery:GetTextColour()
        return self.text_colour or { 0/255 , 255/255 ,255/255 , 1}
    end
    function loramia_com_power_battery:SetTextBoxCustomUpdateFn(fn)
        self.text_box_custom_update_fn = fn
    end
------------------------------------------------------------------------------------------------------------------------------
return loramia_com_power_battery







